<template>
  <!-- 登录组件 -->
  <div class="login-wrap">
    <div class="ms-login">
      <div class="ms-title">物流运输管理系统</div>
      <!-- model：表单数据对象，rules：表单验证规则，ref：给元素或子组件注册引用信息 -->
      <el-form
        :model="param"
        :rules="rules"
        ref="login"
        label-width="0px"
        class="ms-content"
      >
        <!-- 用户名表单 -->
        <el-form-item prop="username" style="margin-bottom: 20px !important;">
          <el-input v-model="param.username" placeholder="username" prefix-icon="el-icon-user">
          </el-input>
        </el-form-item>
        <!-- 密码表单 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="password"
            v-model="param.password"
            prefix-icon="el-icon-lock"
            ><!--  .native - 主要是给自定义的组件添加原生事件 -->
          </el-input>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="submitForm()">登录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
  import request from '../../utils/request.js'
  export default {
    data: function () {
      return {
        param: {
          username: "",
          password: "",
        },
        rules: {
          username: [
            { required: true, message: "请输入用户名", trigger: "blur" },
          ],
          password: [
            { required: true, message: "请输入密码", trigger: "blur" },
          ],
        },
      };
    },
    methods: {
      submitForm() {
        // 获取 ref 注册的引用信息
        this.$refs.login.validate((valid) => {
          if (valid) {
            request.get("/login/userLogin?username=" + this.param.username + "&password=" + this.param.password).then(res=>{
                if (res.code === 200) {
                    this.$store.commit("updateUser", res.data);
                    this.$message.success("登录成功");
                    this.handleAdd({name:'主页', path:'/index'})
                    this.$router.push("/index");
                } else {
                    this.$message.error(res.message);
                }                          
            });
          } else {
            this.$message.error("请输入账号和密码");
            return false;
          }
        });
      },
      /*选中菜单后，把选中菜单信息加入到vuex的标签数组中*/
      handleAdd(val) {
          let flag = true;
          for (let i = 0; i < this.$store.state.tabList.length; i ++) {
              if (val.path == this.$store.state.tabList[i].path) {
                  flag = false;
              }
          }
          if (flag)
              this.$store.dispatch('ADD_TAGS', val);
      }
    },
  };
</script>

<style scoped>
  .login-wrap {
    position: relative;
    width: 100%;
    height: 100vh;
    background-image: url(../../assets/EJB9eWQqYx.jpg);
    background-size: 100%;
  }
  .ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
  }
  .ms-login {
    position: absolute;
    left: 40%;
    top: 25%;
    width: 22vw;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
  }
  .ms-content {
    padding: 30px 30px;
  }
  .login-btn {
    text-align: center;
    margin-top: 20px !important;
  }
  .login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
  }
  .login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
  }
</style>